#!/bin/bash

function cpu() {
	used=$(vmstat | awk '{if(NR==3) print $13 + $14}')
	iowait=$(vmstat | awk '{if(NR==3) print $16}')
	echo "CPU - 使用率: ${used}% 等待磁盘IO响应使用率: ${iowait}%"
}

function memory() {
	total=$(free -m | awk '{if(NR==2) printf "%.1f", $2/1024}')	
	used=$(free -m | awk '{if(NR==2) printf "%.1f", ($2-$NF)/1024}')	
	available=$(free -m | awk '{if(NR==2) printf "%.1f", $NF/1024}')	
	echo "内存 - 总大小: ${total}G, 已使用: ${used}G, 剩余: ${available}G"
}

function disk() {
	fs=$(df -h | awk '/^\/dev/{print $1}')
	for p in $fs; do
		mounted=$(df -h | awk -v p=$p '$1==p{print $NF}')
		size=$(df -h | awk -v p=$p '$1==p{ print $2}')
		used=$(df -h | awk -v p=$p '$1==p{print $3}')
		used_rate=$(df -h | awk -v p=$p '$1==p{print $5}')
		echo "硬盘 - 挂载点: $mounted , 总大小: $size , 已使用: $used, 使用率: $used_rate"
	done
}

tcp_status(){
	summary=$(netstat -antp | sed '1,2d' | awk '{a[$6]++}END{for(i in a) printf i": "a[i]" "}')
	echo "TCP连接状态 - $summary"
}

cpu
memory
disk
tcp_status
